#include "hexconvertor.h"

HexConvertor::HexConvertor( )
{

   // qDebug()<<sizeof(strSrc);
}


QByteArray HexConvertor::hexStringtoHex(QString strSrc)
{
    bool ok;
    QByteArray dataArray;
    char t;

    //剔除开始结尾的空白字符
   strSrc =  strSrc.simplified();

    while(strSrc.length())
    {

        strSrc.remove(QRegExp("^[ \t\n]*"));
        //取两个字符，并转换成16进制数
        t = strSrc.leftJustified(2,' ', true).toUInt(&ok,16);
        dataArray.append(t);

        //删除已经取出的字符
        strSrc.remove(0,2);

    }
    return dataArray;
}

//删除接收到数据中的回车字符'/r'，返回字符串
QString HexConvertor::rmReturn(QByteArray arraySrc)
{
  QString str(QString::fromLocal8Bit(arraySrc));

  str.remove('\r');
  return str;
}

//16进制数转换成以空格隔开的十六进制字符串
QString  HexConvertor::hextoHexString(QByteArray data)
{
    QString str;
    QString byte_str;

    foreach (quint8 a, data) {

       byte_str = QString("%1").arg(a,2,16,QChar('0')).toUpper()+" ";
       str.append(byte_str);   
    }
  //  qDebug()<<str;
    return str;
}

//字符串转16进制字符串
 QString HexConvertor::stringtoHexStr(QString strSrc)
{

}
//16进制字符串转字符串
 QString HexConvertor::hexStrtoString(QString strSrc)
{

    QByteArray data_hex = hexStringtoHex(strSrc);

    //删除16进制数据中的，字符串中间不允许有0出现
    int zeroIndex = data_hex.indexOf('\0');
    while(zeroIndex>=0)
    {
        data_hex.remove(zeroIndex,1);
        zeroIndex = data_hex.indexOf('\0',zeroIndex);
    }


    QString data_str = QString::fromLocal8Bit(data_hex);
    return data_str;

}
